package org.swidgets.base.spaginator;

import org.swidgets.base.IsPaginable;
import org.swidgets.base.sinputbox.SIntegerInputBox;
import org.swidgets.base.slabel.SLabel;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public class SPaginator extends Composite{
	private int recordsPerPage=10;
	private int firstRecord=0; 
	private int curRecordsDisplayed=0;
	private int curPage=0;
	private int numberOfPages=-1;
	private int totalRecords=-1;
	private SIntegerInputBox lblCurPage=new SIntegerInputBox();
	private SLabel lblMaxPage=new SLabel("0");
	private SLabel lblPage=new SLabel("Page");
	private SLabel lblRecordsInfo=new SLabel("0");
	private Button cmdFirst=new Button("<<");
	private Button cmdPrevious=new Button("<-");
	private Button cmdNext=new Button("->");
	private Button cmdLast=new Button(">>");
	private final IsPaginable owner;
	
	public HorizontalPanel paginator=new HorizontalPanel();
	
	private static final boolean isDesignTime() {
		return false;
	}
	
	private void refreshInfo(){
		lblCurPage.setText(""+this.curPage);
		if(this.totalRecords>0){
			lblMaxPage.setText("of "+this.numberOfPages);
			lblRecordsInfo.setText("Displaying records "+(1+this.firstRecord) + "-" + (this.firstRecord+this.curRecordsDisplayed) + " of " + this.totalRecords);
		}
		else{
			lblMaxPage.setText("");
			lblRecordsInfo.setText("Displaying records "+(1+this.firstRecord) + "-" + (this.firstRecord+this.curRecordsDisplayed));
		}
		
	}
	
	private void refreshCmds(){
		if(this.totalRecords>0){
			this.cmdLast.setEnabled(false);
		}
		else{
			this.cmdLast.setEnabled(true);
		}
		if(this.firstRecord==0){
			this.cmdPrevious.setEnabled(false);
			this.cmdFirst.setEnabled(false);
		}
		else{
			this.cmdPrevious.setEnabled(true);
			this.cmdFirst.setEnabled(true);
		}
		if(this.curPage==this.numberOfPages){
			this.cmdLast.setEnabled(false);
			this.cmdNext.setEnabled(false);
		}
		else{
			this.cmdLast.setEnabled(true);
			this.cmdNext.setEnabled(true);
		}
		if(this.numberOfPages==-1){
			this.cmdLast.setEnabled(false);
		}
		
	}
	
	public void refresh(int recordsDisplayed,int totalRecords){
		this.curRecordsDisplayed=recordsDisplayed;
		this.totalRecords=totalRecords;
		if(this.totalRecords>0){
			this.numberOfPages=(int)Math.ceil((double)this.totalRecords/(double)this.recordsPerPage);
		}
		else
		{
			this.numberOfPages=-1;
		}
		this.curPage=1+(int)Math.ceil((double)this.firstRecord/(double)this.recordsPerPage);
		this.refreshInfo();
		this.refreshCmds();
	}
	
	private void initSPaginator(){
		initWidget(paginator);
		paginator.add(cmdFirst);
		cmdFirst.setWidth("20px");
		paginator.add(cmdPrevious);
		cmdPrevious.setWidth("20px");
		paginator.add(lblPage);
		paginator.add(lblCurPage);
		paginator.add(lblMaxPage);
		paginator.add(cmdNext);
		cmdNext.setWidth("20px");
		paginator.add(cmdLast);
		cmdLast.setWidth("20px");
		paginator.add(lblRecordsInfo);
		lblCurPage.setWidth("40px");
		this.paginator.setStyleName("SPaginator");
		this.lblPage.setStyle(lblPage.SMALL_LABEL_STYLE);
		this.lblMaxPage.setStyle(lblMaxPage.SMALL_LABEL_STYLE);
		this.lblRecordsInfo.setStyle(lblRecordsInfo.SMALL_LABEL_STYLE);
		
		refreshInfo();
		if(isDesignTime()){
			return;
		}
		
		cmdFirst.addClickListener(new ClickListener(){

			public void onClick(Widget sender) {
				setFirstRecord(0);
				owner.paginatorReload();
			}
			
		});
		cmdPrevious.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				firstRecord-=recordsPerPage;
				if(firstRecord<0)
					firstRecord=0;
				owner.paginatorReload();
			}
			
		});
		cmdNext.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				firstRecord+=recordsPerPage;
				owner.paginatorReload();
			}
			
		});
		cmdLast.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				firstRecord=totalRecords-recordsPerPage;
				owner.paginatorReload();
			}
			
		});
	}
	
	public SPaginator(){
		super();
		this.owner=new IsPaginable(){
			public void paginatorReload() {};
		};
		this.initSPaginator();
	}
	
	public SPaginator( IsPaginable owner){
		super();
		this.initSPaginator();
		if(owner!=null)
			this.owner=owner;
		else{
			this.owner=new IsPaginable(){
				public void paginatorReload() {};
			};
		}
			
		
	}
	
	public int getRecordsPerPage() {
		return recordsPerPage;
	}
	public void setRecordsPerPage(int rowsPerPage) {
		this.recordsPerPage = rowsPerPage;
	}
	public int getFirstRecord() {
		return firstRecord;
	}
	public void setFirstRecord(int firstRow) {
		this.firstRecord = firstRow;
	}
	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}


	public int getCurRecordsDisplayed() {
		return curRecordsDisplayed;
	}


	public void setCurRecordsDisplayed(int curRecordsDisplayed) {
		this.curRecordsDisplayed = curRecordsDisplayed;
	}


	public int getTotalRecords() {
		return totalRecords;
	}


	public void setTotalRecords(int totalRecords) {
		this.totalRecords = totalRecords;
	}
	
}
